<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('部门树选择')" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div id="tree" class="ztree treeselect"></div>

</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />

<script th:inline="javascript">

    var serverId = [[${id}]];

    $(function() {
        $.modal.loading("数据加载中.....")

        // zTree 配置
        var setting = {
            view: {
                showLine: true,    // 显示连线
                selectedMulti: false
            },
            async: {
                enable: true,        // 启用异步加载
                url: ctx + "server/filePath", // 后端接口
                autoParam: ["id","pid","allPath"],   // 传递当前节点的 id
                otherParam: {"serverId": serverId}, // 可选的其他参数
                dataType: "json",    // 返回数据格式
                dataFilter: function(treeId, parentNode, responseData) {

                    // 处理响应数据
                    $.modal.closeLoading();
                    // 防止每次都调用
                    if (responseData.data && !parentNode ) {
              // 标记该节点已处理
                        console.log("Processing data for node");
                        $.modal.closeLoading();
                        return responseData.data;  // 处理并返回数据
                    }
                    return null;
                }
            },
            callback: {
                onClick: function(event, treeId, treeNode) {
                    // 点击节点时，异步加载该节点的子节点，并传递当前节点数据到后端
                    if (treeNode.isParent) {
                        var zTree = $.fn.zTree.getZTreeObj("tree");
                        // 将节点信息传递给后端
                        $.ajax({
                            url: ctx + "server/filePath",
                            method: "Post",
                            data: { prePath: treeNode.id,allPath:treeNode.allPath,serverId:serverId }, // 传递当前节点的 ID
                            success: function(data) {
                                debugger
                                // 1.获取到子节点
                                var hasChildIds = [];
                                $.each(treeNode.children,function (index,value) {
                                    hasChildIds.push(value.id);
                                })

                                var relData = [];
                                $.each(data.data,function (index,value) {
                                   if(!hasChildIds.includes(value.id)){
                                       relData.push(value);
                                   }
                                })

                                zTree.addNodes(treeNode, relData);
                            }
                        });
                    }
                }
            }
        };

        // 初始化 zTree
        $.fn.zTree.init($("#tree"), setting);

    });

</script>
</body>
</html>